package com.javasm.dao.MisMannageDao.impl;


import com.javasm.dao.MisMannageDao.BuyFundDao;
import com.javasm.entity.MisMannageEntity.BuyFund;
import com.javasm.util.JDBCUtil;

import java.math.BigDecimal;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @author:JAVASM
 * @description:
 * @date:2022/5/26 11:11
 * @version:0.1
 * @since:jdk11
 */
public class BuyFundDaoImpl implements BuyFundDao {

    @Override
    public Integer getFundTotalNum(String prodIds) {
        Connection conn = JDBCUtil.getConn();
        PreparedStatement ps = null;
        ResultSet rs = null;

        StringBuilder sb = new StringBuilder();
        sb.append("select count(1) as totalNum\n" +
                "from tb_prod_info tpi,tb_prod_series tps,tb_type tt\n" +
                "where tpi.series_id=tps.series_id \n" +
                "and tpi.second_type_id=tt.type_id\n" +
                "and tpi.prod_id in ("+prodIds+")\n");
        Integer totalNum = 0;
        try {
            ps = conn.prepareStatement(sb.toString());
            rs = ps.executeQuery();
            while (rs.next()) {
                totalNum = rs.getInt("totalNum");
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
           JDBCUtil.closeConn(conn, ps,null, rs);
        }
        return totalNum;
    }

    @Override
    public List<Integer> getUpProdId() {
        Connection conn = JDBCUtil.getConn();
        PreparedStatement ps = null;
        ResultSet rs = null;

        StringBuilder sb = new StringBuilder();
        sb.append("select show_prod_id from tb_show where subscribe_upwards=1");

        List<Integer> prodIdList = new ArrayList<Integer>();
        try {
            ps = conn.prepareStatement(sb.toString());
            rs = ps.executeQuery();
            while (rs.next()) {
                Integer showProdId = rs.getInt("show_prod_id");
                prodIdList.add(showProdId);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return prodIdList;
    }

    @Override
    public List<BuyFund> getFundByPage(Integer page, Integer pageSize, Integer typeId, String prodIds) {
        Connection conn = JDBCUtil.getConn();
        PreparedStatement ps = null;
        ResultSet rs = null;

        StringBuilder sb = new StringBuilder();
        sb.append("select tpi.prod_id,tpi.prod_name,tpi.series_id,tps.series_CHname,tt.type_name,tpi.opening_time,tpi.money_type,tpi.subscribe_rate,\n" +
                "tpi.fund_manage_rate,tpi.min_money,tpi.money_submit_style,tpi.redeem_cycle,tpi.redeem_fee,tpi.closed_cycle\n" +
                "from tb_prod_info tpi,tb_prod_series tps,tb_type tt\n" +
                "where tpi.series_id=tps.series_id \n" +
                "and tpi.second_type_id=tt.type_id\n" +
                "and tpi.prod_id in ("+prodIds+")\n"+
                "limit "+(page-1)*pageSize+","+pageSize);
        List<BuyFund> buyFundList = new ArrayList<BuyFund>();
        try {
            ps = conn.prepareStatement(sb.toString());
            rs = ps.executeQuery();
            while (rs.next()) {
                Integer prodId = rs.getInt("prod_id");
                String prodName = rs.getString("prod_name");
                Integer seriesId = rs.getInt("series_id");
                String seriesCHname = rs.getString("series_CHname");
                String typeName = rs.getString("type_name");
                Date openingTime = rs.getDate("opening_time");
                String moneyType = rs.getString("money_type");
                String subscribeRate = rs.getString("subscribe_rate");
                String fundManageRate = rs.getString("fund_manage_rate");
                BigDecimal minMoney = rs.getBigDecimal("min_money");
                String moneySubmitStyle = rs.getString("money_submit_style");
                Integer redeemCycle = rs.getInt("redeem_cycle");
                Integer redeemFee = rs.getInt("redeem_fee");
                Integer closedCycle = rs.getInt("closed_cycle");

                BuyFund buyFund = new BuyFund(prodId,prodName,seriesId,seriesCHname,typeName,openingTime,moneyType,subscribeRate,fundManageRate,minMoney,moneySubmitStyle,redeemCycle,redeemFee,closedCycle);
                buyFundList.add(buyFund);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
           JDBCUtil.closeConn(conn, ps,null, rs);
        }
        return buyFundList;
    }
}
